package view.drawable;

import model.Arc;
import test.Test;

import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Thomas
 * Date: 08/02/14
 * Time: 11:35
 * To change this template use File | Settings | File Templates.
 */
public class ArcDrawable extends FormDrawable {

    private Arc arc;
    private boolean selected = false;
    private int position_draw_tram = -1;
    private int nb_points_arc = 0;

    /**
     * @param pos   the coordinate of the center this form
     * @param dim   the dimension of this form
     */
    public ArcDrawable(Point pos, Dimension dim, Arc arc) {
        super(Color.GREEN, pos, dim);
        this.setArc(arc);
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public Arc getArc() {
        return arc;
    }

    public void setArc(Arc arc) {
        this.arc = arc;
    }

    public int getPosition_draw_tram() {
        return position_draw_tram;
    }

    public void setPosition_draw_tram(int position_draw_tram) {
        this.position_draw_tram = position_draw_tram;
    }

    public int getNb_points_arc() {
        return nb_points_arc;
    }

    @Override
    public void draw(Graphics g) {
        if (this.isDrawing()) {
            return;
        }

        Color c;
        int fleche_size = 5;
        String ligne = this.getArc().getLigne();
        if (Test.getGraphColor().getColors().containsKey(ligne)) {
            c = Test.getGraphColor().getColors().get(ligne);
        } else {
            c = Color.GRAY;
        }
        g.setColor(c);
        NoeudDrawable source = this.getArc().getSource().getNoeudDrawable();
        NoeudDrawable destination = this.getArc().getDestination().getNoeudDrawable();

        Graphics2D g2 = (Graphics2D)g;
        if (this.isSelected()) {
            g2.setStroke(new BasicStroke(10));
        } else {
            g2.setStroke(new BasicStroke(2));
        }
        //List<GenerateGraphicalArc.PointF> points = GenerateGraphicalArc.generateCurve(new GenerateGraphicalArc.PointF(source.getPosition()), new GenerateGraphicalArc.PointF(destination.getPosition()), 185f, 10f, true, true);
        List<GenerateGraphicalArc.PointF> points = GenerateGraphicalArc.generateLine(new GenerateGraphicalArc.PointF(source.getPosition()), new GenerateGraphicalArc.PointF(destination.getPosition()));

        // Calculate the bounds of the curve
        Rectangle2D.Float bounds = new Rectangle2D.Float();
        for (int i = 1; i < points.size(); ++i) {
            bounds.add(points.get(i).x, points.get(i).y);
        }

        this.nb_points_arc = points.size();
        this.setPosition_draw_tram(Math.min(this.getPosition_draw_tram(),points.size()-1));

        int x = source.getPosition().x;
        int y = source.getPosition().y;
        int x2 = destination.getPosition().x;
        int y2 = destination.getPosition().y;

        if (y - y2 >= x - x2) {
            x += 4;
            y += 4;
            x2 += 4;
            y2 += 4;
        } else {
            x -= 4;
            y -= 4;
            x2 -= 4;
            y2 -= 4;
        }

        double distance = Math.sqrt(Math.pow((y2 - y),2) + Math.pow((x2 - x), 2));
        if (distance < 500) {
            Line2D.Float f = new Line2D.Float(x,y,x2,y2);
            g2.draw(f);
        }

        for (int i = 0; i < points.size(); ++i) {
            if (i > 0) {
                //Line2D.Float f = new Line2D.Float(points.get(i - 1).x, points.get(i - 1).y, points.get(i).x, points.get(i).y);
                //g2.draw(f);

                //affichage des fleches
                if (i == points.size()/2) {
                    g.drawString(this.getArc().getCout()+"", (int) points.get(i).x, (int) points.get(i).y);
                    float distance_x = points.get(0).x - points.get(points.size() - 1).x;
                    float distance_y = points.get(0).y - points.get(points.size() - 1).y;
                    //si on se deplace horizontalement
                    if (Math.abs(distance_x) > Math.abs(distance_y)) {
                        //si on va vers la droite
                        if (distance_x < 0) {
                            g.drawPolygon(new Polygon(new int[]{(int) points.get(i).x, (int) (points.get(i).x), (int) points.get(i).x + fleche_size},
                                    new int[]{(int) (points.get(i).y-fleche_size), (int) (points.get(i).y + fleche_size), (int) points.get(i).y}, 3));
                        } else {
                            g.drawPolygon(new Polygon(new int[]{(int) points.get(i).x, (int) (points.get(i).x), (int) points.get(i).x - fleche_size},
                                    new int[]{(int) (points.get(i).y-fleche_size), (int) (points.get(i).y + fleche_size), (int) points.get(i).y}, 3));
                        }
                    } else {
                        //si on va vers la bas
                        if (distance_y < 0) {
                            g.drawPolygon(new Polygon(new int[]{(int) points.get(i).x - fleche_size, (int) (points.get(i).x + fleche_size), (int) points.get(i).x},
                                    new int[]{(int) (points.get(i).y), (int) (points.get(i).y), (int) points.get(i).y + fleche_size}, 3));
                        } else {
                            g.drawPolygon(new Polygon(new int[]{(int) points.get(i).x - fleche_size, (int) (points.get(i).x + fleche_size), (int) points.get(i).x},
                                    new int[]{(int) (points.get(i).y), (int) (points.get(i).y), (int) points.get(i).y - fleche_size}, 3));
                        }
                    }
                }
            }
            if (this.getPosition_draw_tram() == i) {
                Image img = Toolkit.getDefaultToolkit().getImage("./ressources/tram.png");
                if (this.getArc().getLigne().equals("0")) {
                    img = Toolkit.getDefaultToolkit().getImage("./ressources/pieton.png");
                }
                g.drawImage(img, (int)points.get(i).x - 70/2, (int)points.get(i).y - 35/2, 70, 35, null);
            }

        }
    }
}
